/******************************************************
  File: src/msgcodes.c
  Description: Audio Cup Message Codes Loop ( object )
  Author: Brant Anderson
  Date: 10/29/01
  Last Update: 12/22/01
  Status: Needs work
  To Do: -Config-file junk, Key-Press Events
 ******************************************************/



#include <stdio.h>
#include <unistd.h>
#include "../include/msgcodes.h"


int msgCodes( int sock ) {
	static int status = 0;
    static unsigned long duration = 0;

    static SongInfo songinf;
	static XA_Message xaMessage;
	int tmpsongnum = 0;
    songinf.songnum = tmpsongnum;
    songinf.playlist = "/home/brant/.acup/default.m3u";

	/** Temperary until config file can be read **/
	songinf.filename = getSong( songinf.playlist, songinf.songnum );
	printf( "Song Playing: %s\n", songinf.filename );

    songinf = getSongInfo( songinf ); // NEED TO ADD: ctype detection in this function

	songinf.songnum = 0;
    songinf.loop = 1;

	menuDisplay( songinf );
	playSong( songinf.ctype );


	while( songinf.loop ) {    /* create infinate loop terminated on exit */

		/* return int for error catching */
		songinf = keySignal( sock, songinf ); // Need to send this the playlist to be played.

		if( songinf.ctype == MP3 ) {
			status = getMessage( songinf.ctype, &xaMessage ); /* Find a way to get this by passing ctype */
			songinf = keySignal( sock, songinf );
		} /* else if( ctype == OGG ) {...} */
		else {
			printf("Error: ctype is: %d\n", songinf.ctype);
		}

		if( songinf.ctype == MP3 ) {

			do {
				songinf = keySignal( sock, songinf );
				if ( status < 0 ) {

					if ( status == XA_ERROR_PIPE_CLOSED ) {
						/* display the error message */
						printf( "Help!\nXA_ERROR_PIPE_CLOSED-XAPLAYER: decoder process exited. Exiting.\n" );
						/* and exit */
						songinf.loop = 0;
					} else {
						printf( "Help!\nXAPLAYER: error in message_wait()\n" );
						songinf.loop = 0;
					}

				} else if( status == 0 ) {

					usleep(500);
					songinf = keySignal( sock, songinf );

				} else {

					switch ( xaMessage.code ) {
						 case XA_MSG_NOTIFY_READY:
							//printf( "XA_MSG_NOTIFY_READY\n" );
							break;

						  case XA_MSG_NOTIFY_EXITED:
							//printf( "XA_MSG_NOTIFY_EXITED\n" );
							break;

						  case XA_MSG_NOTIFY_INPUT_CAPS:
							//printf( "XA_MSG_NOTIFY_INPUT_CAPS\n" );
							break;

						  case XA_MSG_NOTIFY_PLAYER_STATE:
							//printf( "XA_MSG_NOTIFY_PLAYER_STATE\n" );
							switch ( xaMessage.data.state ) {
								case XA_PLAYER_STATE_PLAYING:
									//printf( "   XA_PLAYER_STATE_PLAYING\n" );
									break;

								case XA_PLAYER_STATE_PAUSED:
									//printf( "   XA_PLAYER_STATE_PAUSED\n" );
									break;

								case XA_PLAYER_STATE_STOPPED:
									//printf( "   XA_PLAYER_STATE_STOPPED\n" );
									break;

								case XA_PLAYER_STATE_EOS:
									//printf( "   XA_PLAYER_STATE_EOS\n" );
								    songinf = right_signal_connect( songinf );
    								break;

								default:
									break;
							}
							break;

						case XA_MSG_NOTIFY_ACK:
							//printf( "XA_MSG_NOTIFY_ACK\n" );
							if (xaMessage.data.ack == XA_MSG_COMMAND_INPUT_OPEN) {
								//printf( "   XA_MSG_COMMAND_INPUT_OPEN\n" );
							}
							break;

						case XA_MSG_NOTIFY_INPUT_POSITION:
							songinf = keySignal( sock, songinf );
							// printf( "XA_MSG_NOTIFY_INPUT_POSITION\n" );
							break;

						case XA_MSG_NOTIFY_INPUT_TIMECODE: // MUST send ELAPSE time to determine BAR GRAPH LENGTH!
						    if( duration ) {
			                    menuDisplayTime( xaMessage.data.timecode.m, xaMessage.data.timecode.s, duration );
			                }
							// printf( "Time: %.2d:%.2d\n", xaMessage.data.timecode.m, xaMessage.data.timecode.s );
							break;

						case XA_MSG_NOTIFY_STREAM_DURATION:
						    duration = xaMessage.data.duration;
						    printf( "Duration of Song: %li\n", duration );
							//printf( "XA_MSG_NOTIFY_STREAM_DURATION: %li:%li\n", xaMessage.data.duration, duration );
							break;

						case XA_MSG_NOTIFY_STREAM_PARAMETERS:
							//printf( "XA_MSG_NOTIFY_STREAM_DURATION\n" );
							break;

						case XA_MSG_NOTIFY_OUTPUT_CAPS:
							//printf( "XA_MSG_NOTIFY_OUTPUT_CAPS\n" );
							break;

						case XA_MSG_NOTIFY_OUTPUT_BALANCE:
							//printf( "XA_MSG_NOTIFY_OUTPUT_BALANCE\n" );
							break;

						case XA_MSG_NOTIFY_OUTPUT_PCM_LEVEL:
							//printf( "XA_MSG_NOTIFY_OUTPUT_PCM_LEVEL\n" );
							break;

						case XA_MSG_NOTIFY_OUTPUT_MASTER_LEVEL:
							//printf( "XA_MSG_NOTIFY_OUTPUT_MASTER_LEVEL\n" );
							break;

						case XA_MSG_NOTIFY_ERROR:
							//printf( "XA_MSG_NOTIFY_ERROR\n" );
							break;

						case XA_MSG_NOTIFY_DEBUG:
							//printf( "XA_MSG_NOTIFY_DEBUG\n" );
							printf( "XAPLAYER: debug message (source=%d,level=%d): %s\n",
															xaMessage.data.debug.source,
															xaMessage.data.debug.level,
															xaMessage.data.debug.message );

						    songinf = right_signal_connect( songinf );

							return 0;
							break;

						default:
							break;

					}

				}
					/** Have to find a way to make xaMessage Generic **/
			} while( songinf.loop == 1 && songinf.ctype  == MP3 && (status = getMessage( songinf.ctype, &xaMessage )) != 0 );

		} /* else if( songinf.ctype == OGG ) {...} */

	}
	return songinf.loop;
}
